package csci5115.autoalarm;

import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class EventDBAdapter {
	
	public static final String DB_NAME = "events.db";
	public static final String TABLE_NAME = "events";
	public static final String KEY_ID = "_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_LOCATION = "location";
	public static final String KEY_DATE = "date";
	public static final String KEY_DAY = "day";
	public static final String KEY_MONTH = "month";
	public static final String KEY_ALARM_STATUS = "alarm_status";
	public static final String KEY_TRANSPORTATION = "transportation";
	public static final String KEY_ALARM_TYPE = "alarm_type";

	private static final int DATABASE_VERSION = 2;

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    
    private final Context mCtx;
    
    private static class DatabaseHelper extends SQLiteOpenHelper {

    	public DatabaseHelper(Context context) {
    		super(context, DB_NAME, null, DATABASE_VERSION);
    	}
    	
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("create table if not exists " + TABLE_NAME + " ( " 
					+ KEY_ID + " integer primary key autoincrement, "
					+ KEY_TITLE + " text not null, "
					+ KEY_DATE + " long not null, "
					+ KEY_DAY + " integer not null, "
					+ KEY_MONTH + " integer not null, "
					+ KEY_ALARM_STATUS + " integer not null, "
					+ KEY_ALARM_TYPE + " text not null, "
					+ KEY_TRANSPORTATION + " byte not null, "
					+ KEY_LOCATION + " text not null);"); 
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
		}
    }
    
	public EventDBAdapter(Context context) {
		mCtx = context;
	}

	public EventDBAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public void delete(int mId) {
    	mDb.delete(TABLE_NAME, " _id=?", new String[] {Integer.toString(mId) });
    }
	public void insert(AAEvent e){
		mDb.insert(TABLE_NAME, null, e.toDB());
	}
	
	public void replace(AAEvent e){
		String id = Integer.toString(e.getID());
		mDb.delete(TABLE_NAME, KEY_ID + "=?", new String[] { id });
		mDb.replace(TABLE_NAME, null, e.replaceDB());
	}
	
	public Cursor getAllEvents() {

		String fields[] = { "_id", "title", "date", "day", "month", "alarm_status",
				"alarm_type", "transportation", "location" };
		return mDb.query(TABLE_NAME, fields, null, null, null, null, "date asc");
	}
		
	public Cursor getEventsOn(Date d1) {
		String day = Integer.toString(d1.getDate());
		String month = Integer.toString(d1.getMonth() + 1);
		return mDb.rawQuery("select * from events where month =? AND day =?", new String[] {month, day});
	}
}
